<template>
	<view>
		<u--form labelPosition="left" :model="model1" :rules="rules" ref="form1" labelWidth=25% labelAlign="center">
			<u-form-item label="用户名" prop="userInfo.account" borderBottom ref="item1">
				<u--input v-model="model1.userInfo.account" border="none" placeholder="请填写新用户名"></u--input>
			</u-form-item>
			<u-form-item label="原密码" prop="userInfo.password" borderBottom>
				<u--input v-model="model1.userInfo.password" border="none" placeholder="请填写原密码(只修改用户名可不填)"></u--input>
			</u-form-item>
			<u-form-item label="新密码" prop="userInfo.newpassword" borderBottom ref="item2">
				<u--input v-model="model1.userInfo.newpassword" border="none" placeholder="请填写新密码"></u--input>
			</u-form-item>
		</u--form>

		<u-gap height="50" bgColor="#ffffff"></u-gap>

		<u-button type="primary" text="提交" @click="updateinfo"></u-button>		

	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList1: [],
				show: false,				
				model1: {
					userInfo: {
						account: '',
						password: '',
						newpassword: '',
					},
				},
				rules: {
					'userInfo.account': {
						type: 'string',
						min: 2,
						max: 10,
						required: true,
						message: '长度在2-10之间',
						trigger: ['change', 'blur']
					},
					'userInfo.newpassword': {
						type: 'string',
						min: 6,
						max: 20,
						
						
						message: '长度在6-20之间',
						trigger: ['change', 'blur']
					},
				}	
			};
		},
		methods: {
			updateinfo() {
				// this.$axios.post("/yz/tologin", this.model1.userInfo).then(res => {
				// 	if (res.data.code == 200) {
				// 		uni.$u.toast('提交成功');
				// 		uni.switchTab({
				// 			url: "/pages/my/my"
				// 		})
				// 	}else{
				// 		uni.$u.toast('原密码错误，请重新输入！');
				// 	}
				// });
				
				
				
				this.$refs.form1.validate().then(res => {
					if(this.model1.userInfo.newpassword == ''){
						this.$axios.post("/yz/doupdate", this.model1.userInfo).then(res => {
							if (res.data.code == 200) {
								uni.$u.toast('修改成功！');
								setTimeout(() => {
									uni.switchTab({
										url: "/pages/my/my"
									})
								}, 1000)
							}
							});
					}else{
						this.$axios.post("/yz/tologin", this.model1.userInfo).then(res => {
							if (res.data.code == 200) {
								this.$axios.post("/yz/doupdate", this.model1.userInfo).then(res => {
									if (res.data.code == 200) {
											uni.$u.toast('修改成功,请重新登录！');
											setTimeout(() => {
											uni.navigateTo({
												url:"/pages/login/login"
											})
											}, 1000)
									}
								});
							}else{
								uni.$u.toast('原密码错误，请重新输入！');
							}
						});
					}
					
					
					
					
					
					
				}).catch(errors => {
					this.show = true;
					uni.$u.toast('请正确填写信息！')
				})

			}
		},
	}
</script>

<style lang="scss">

</style>
